//
// Created by chizuru on 2025/9/2.
//

#pragma once
#include <tinyxml2.h>
#include <string>
#include <unordered_map>
#include <optional>
#include <stdexcept>
#include <sstream>
#include <cctype>



/*
 * TinyXmlRegisterCLBMap
 *
 * - Keying: hash = FNV-1a 64-bit of "category.name"
 * - map: unordered_map<uint64_t, vector<RegInfo>> to handle collisions (vector scan)
 * - RegInfo contains address, tooltip, description, category, name
 * - XMLDocument doc is stored as member so node pointers remain valid.
 */
class TinyXmlRegisterCLBMap {
public:
    struct RegInfo {
        uint16_t address;
        std::string tooltip;
        std::string description;
        std::string category;
        std::string name;
    };

    // load from file path; throws on fatal errors
    explicit TinyXmlRegisterCLBMap(const std::string& xmlPath) ;
    // returns optional address
    std::optional<uint16_t> getAddress(const std::string& category, const std::string& name) const ;

    // returns optional RegInfo (copy)
    std::optional<RegInfo> getInfo(const std::string& category, const std::string& name) const ;

    // expose hash function in case caller needs it
    static uint64_t hashKey(const std::string& category, const std::string& name);

private:
    tinyxml2::XMLDocument doc; // keep DOM alive
    std::unordered_map<uint64_t, std::vector<RegInfo>> map;
};

